package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller//设定SpringMvc的核心控制器bean
//@RequestMapping("/user")
public class UserController {

    /*@RequestMapping("/save")
    @ResponseBody//设置当前控制器方法响应内容为当前返回值，无需解析
    public String save(){
        System.out.println("user save ...");
        return "{'module':'user save'}";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(){
        System.out.println("user delete ...");
        return "{'module':'user delete'}";
    }*/

   /* //GET/POST返回参数
    @RequestMapping("/commonParam")
    @ResponseBody
    public String commonParam(String name,int age){
        //Get发送单个请求
        System.out.println("普通参数传递 name ==> "+name);
        //Get发送多个参数
        System.out.println("普通参数传递 age ==> "+age);
        return "{'module':'commonParam'}";
    }*/

    /*//普通参数: 形参与地址参数名不一致的情况
    @RequestMapping("/commonParamDifferentName")
    @ResponseBody
    //形参和请求参数名不一致时，使用@RequestParam指定参数名
    public String commonParamDifferentName(@RequestParam("name") String userName, int age){
        System.out.println("普通参数传递 userName ==> "+userName);
        System.out.println("普通参数传递 age ==> "+age);
        return "{'module':'common param differentname'}";
    }*/

    /*//POJO参数：请求参数与形参对象中的属性对应即可完成参数传递
    @RequestMapping("/pojoParam")
    @ResponseBody
    public String pojoParam(User user){
        System.out.println("pojo参数传递 user ==> "+user);
        return "{'module':'pojo param'}";
    }*/

   /* //嵌套POJO参数：请求参数与形参对象中的属性对应即可完成参数传递
    //注意：请求参数key的名称要和POJO中的属性的名称一致，否则无法封装
    @RequestMapping("/pojoParam")
    @ResponseBody
    public String pojoParam(User user){
        System.out.println("pojo参数传递 user ==> "+user);
        return "{'module':'pojo param'}";
    }*/

   /* //数组参数：同名请求参数可以直接映射到对应名称的形参组对象中
    @RequestMapping("/arrayParam")
    @ResponseBody
    public String arrayParam(String[] likes){
        System.out.println("数组参数传递 likes ==> "+ Arrays.toString(likes));
        return "{'module':'array param'}";
    }*/

    /*//集合参数：同名请求参数可以使用@RequestParam注解映射到对应名称集合对象中作为数据
    //注意：集合保存普通参数：请求参数名与形参集合对象名相同且请求参数为多个，@RequestParam绑定参数关系
    //对于简单数据类型使用数组比集合简单点
    @RequestMapping("/listParam")
    @ResponseBody
    public String listParam(@RequestParam List<String> likes){
        System.out.println("集合参数传递 likes ==> "+likes);
        return "{'module':'list param'}";
    }*/

    /*//使用@RequestBody注解将外部传递的json数组数据映射到形参的集合对象中作为数据
    @RequestMapping("/listParamForJson")
    @ResponseBody
    public String listParamForJson(@RequestBody List<String> likes){
        System.out.println("list common(json) 参数传递 list ==> "+likes);
        return "{'module':'list common for json param'}";
    }*/

   /* //Json对象数据
    @RequestMapping("/pojoParamForJson")
    @ResponseBody
    public String pojoParamForJson(@RequestBody User user){
        System.out.println("pojo(json) 参数传递 user ==> "+user);
        return "{'module':'pojo for json param'}";
    }*/

   /* //JSON对象数组
    @RequestMapping("/listPojoParamForJson")
    @ResponseBody
    public String listPojoParamForJson(@RequestBody List<User> list){
        System.out.println("list pojo(json)参数传递 list ==> "+list);
        return "{'module':'list pojo for json param'}";
    }*/

   /* //日期类型参数传递
    @RequestMapping("/dataParam")
    @ResponseBody
    //@DateTimeFormat设定日期时间数据格式
    public String dataParam(Date date, @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
                            @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date date2){
        System.out.println("参数传递 date ==> "+date);
        System.out.println("参数传递 date1(yyyy-MM-dd) ==> "+date1);
        System.out.println("参数传递 date2(yyyy/MM/dd HH:mm:ss) ==> "+date2);
        return "{'module':data param'}";
    }*/

    /*//响应JSON数据
    @RequestMapping("/toJsonPOJO")
    @ResponseBody
    public User toJsonPOJO(){
        System.out.println("返回json对象数据");
        User user = new User();
        user.setName("itcast");
        user.setAge(22);
        return user;
    }*/

    /*//响应PoJo集合对象
    @RequestMapping("/toJsonList")
    @ResponseBody //设置当前控制器返回值作为响应体，写在类上，该类的所有方法都有该注解功能
    public List<User> toJsonList(){
        System.out.println("返回json数据集合");
        User user1 = new User();
        user1.setName("传智播客");
        user1.setAge(15);

        User user2 = new User();
        user2.setName("黑马程序员");
        user2.setAge(12);

        List<User> userList = new ArrayList<User>();
        userList.add(user1);
        userList.add(user2);

        return userList;
    }*/

    //RestFul入门案例
  /*  //设置当前请求方法为POST，表示REST风格中的添加操作
    @RequestMapping(value = "/users",method = RequestMethod.POST)
    @ResponseBody
    public String save(){
        System.out.println("user save ..");
        return "{module':'user save'}";
    }*/

    /*//设置当前请求方法为DELETE，表示REST风格中的删除操作
    @RequestMapping(value = "/users/{id}/{name}",method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id,@PathVariable String name) {
        System.out.println("user delete..." + id+","+name);
        return "{'module':'user delete'}";
    }*/

    /*//设置当前请求方法为PUT，表示REST风格中的修改操作
    @RequestMapping(value = "/users",method = RequestMethod.PUT)
    @ResponseBody
    public String update(@RequestBody User user){
        System.out.println("user update ..."+user);
        return "{'module':'user update'}";
    }*/

    /*//设置当前请求方法为GET，表示REST风格中的查询操作
    @RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String getById(@PathVariable Integer id){
        System.out.println("user getById ..."+id);
        return "{'module':user getById'}";
    }*/

   /* //设置当前请求方法为GET，表示REST风格中的查询操作
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    @ResponseBody
    public String getAll(){
        System.out.println("user getAll ...");
        return "{'module':'user getAll'}";
    }*/
}
